var cookie_goods_name = 'mall_cart_goods';
var cookie_package_name="mall_cart_package";
var cookie_gift_name ='mall_cart_gift';
var cookie_coupon_name = 'mall_cart_coupon';
var cookie_cart_num = 'mall_cart_num';
var mallDays = 30 ;
var split_str = "--";
var sub_split_str='-';


function updateCoupon(id){
	setCookie(cookie_coupon_name,id,mallDays);
}
function deleteCoupon(){
	delCookie(cookie_coupon_name);
}

function addCartNum(){
	var tmp = getCookie(cookie_cart_num);
	var now = 0;
	if(tmp!=''){
		now = parseInt(tmp);
	}
	now++;
	setCookie(cookie_cart_num,now,mallDays);
}
function subCartNum(){
	var tmp = getCookie(cookie_cart_num);
	var now = 0;
	if(tmp!=''){
		now = parseInt(tmp);
		now--;
		if(now<0){
			now = 0 ;
		}
	}
	
	setCookie(cookie_cart_num,now,mallDays);
}
function clearCartNum(){
	setCookie(cookie_cart_num,0,mallDays);
}
function getCartNum(){
	var tmp = getCookie(cookie_cart_num);
	var now = 0;
	if(tmp!=''){
		now = tmp;
	}
	return now ;
}

function addItem(id,type) {	
	updateItem(id,1,type);
	alert('添加成功');
}
function getCookieName(type) {
	if(type=='goods'){
		return cookie_goods_name;
	}else if(type=='package'){
		return cookie_package_name;
	}else if(type=='gift'){
		return cookie_gift_name;
	}
	return '';
}
//
function containItem(id,type) {
	
	var cookie_name = getCookieName(type);
	var oldValue = getCookie(cookie_name);
 
	if(oldValue ==''){
		return false;
		
	}else{	  		
	  	var p_ids = oldValue.split(split_str);
	  	 
	  	for(var i=0; i<p_ids.length;i++){
	  		var tmp = p_ids[i].split(sub_split_str);
	  		if(tmp[0] == id){	  			 
	  			return true;	  			
	  		} 
	  	}  	  		
	  	return false;
 
	  	
	 }
	   
}
function emptyCart( ) {
	 if(!confirm('是否删除')){
		 	return;
	}
	delCookie(cookie_goods_name);
	delCookie(cookie_package_name);
	delCookie(cookie_gift_name);
	delCookie(cookie_coupon_name);
	clearCartNum();
}

//num==0 表示删除
function connect(oldValue ,id ,num){
	var value='';
	if(oldValue ==''){
		if(num !=0){
			value= id +sub_split_str+num;
		}
		
	}else{	  		
	  	var p_ids = oldValue.split(split_str);
	  	var exist = false;
	  	for(var i=0; i<p_ids.length;i++){
	  		var tmp = p_ids[i].split(sub_split_str);
	  		if(tmp[0] == id){
	  			exist = true;
	  			if(num != 0 ){
	  				value +=    id +sub_split_str+num + split_str;
	  			}
	  			
	  		}else{
	  			value +=    p_ids[i] + split_str;
	  		}
	  	}  	  		
	  		
	  	if(!exist){
	  		if(num!=0){
	  			value += id +sub_split_str+num + split_str;
	  		}
	  		
	  	}
	  	if(value.length > split_str.length){
	  		value = value.substring(0 , value.length-split_str.length);
	  	}
	  	
	  }
	  return value;
}

function updateItem(id,num,type) {
	
	if(!containItem(id,type)){
		addCartNum();
	}
	
	
	var cookie_name = getCookieName(type);
	var value = getCookie(cookie_name);
	var newValue = connect(value,id,num);	
	setCookie(cookie_name,newValue,mallDays);
	
	
}

function deleteItem(id,type) {
	if(!containItem(id,type)){
		subCartNum();
	}
	updateItem(id,0,type);
  	 
}

function setCookie(name,value,days){  
	     var exp  = new Date();  
	     exp.setTime(exp.getTime() + days*24*60*60*1000);  
	     document.cookie = name + "="+ escape (value) + ";expires=" + exp.toGMTString();  
}  
	   
function getCookie(name){  
	var arr = document.cookie.match(new RegExp("(^| )"+name+"=([^;]*)(;|$)"));   
	if(arr != null){  
	    return unescape(arr[2]);  
	}else{  
	    return "";  
	}  
}  
	   
function delCookie(name){  
	var exp = new Date();   
	exp.setTime(exp.getTime() - 1);  
	var cval=getCookie(name);  
	if(cval!=null) document.cookie= name + "="+cval+";expires="+exp.toGMTString();  
}